import request from '@/utils/request'
import type {AxiosPromise} from "axios";
import type {GiteeUser} from "@/data/api/gitee/types";
import type {GiteeFile} from "@/data/entity/GiteeFile";

// https://gitee.com/api/v5/swagger

const author = {
    'author[name]':'lizzii-data',
    'author[email]':'lizzii-data email'
}

export function getUser(access_token:string):AxiosPromise<GiteeUser>{
    return request({
        url: 'https://gitee.com/api/v5/user',
        method: 'get',
        params: { access_token }
    })
}

export function createRepo(access_token:string, name:string){
    const auto_init = true
    return request({
        url: 'https://gitee.com/api/v5/user/repos',
        method: 'post',
        data: { access_token, name, auto_init }
    })
}

export function getFile(access_token:string, owner:string, repo:string, path:string):AxiosPromise<GiteeFile|GiteeFile[]>{

    return request({
        url: '/api/v5/repos/' + owner + '/'+ repo + '/contents/' + path,
        method: 'get',
        params: { access_token }
    })
}

export function putFile(access_token:string, owner:string, repo:string, path:string, content:string, message:string, sha:string){

    return request({
        url: '/api/v5/repos/' + owner + '/'+ repo +'/contents/' + path,
        method: 'put',
        data: { access_token, content, message, sha, ...author }
    })
}

export function postFile(access_token:string, owner:string, repo:string, path:string, content:string, message:string){

    return request({
        url: '/api/v5/repos/' + owner + '/'+ repo +'/contents/' + path,
        method: 'post',
        data: { access_token, content, message, ...author }
    })
}